package com.gdut.simple.data.local.dao;

import android.content.Context;

import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.data.entity.History;
import com.gdut.simple.data.local.db.DbManager;

import java.util.List;

import rx.functions.Action1;

public class HistoryDao {

    public static void queryHistoryList(Context context, final RequestCallback<List<History>> callback) {
        DbManager.queryHistory(context).subscribe(new Action1<List<History>>() {
            @Override
            public void call(List<History> histories) {
                if (histories != null) {
                    if (callback != null) {
                        callback.onSuccess(histories);
                    }
                } else {
                    if (callback != null) {
                        callback.onFailure("查询出错");
                    }
                }
            }
        });
    }

    public static void querySingleHistory(Context context, String title, String url, final RequestCallback<History> callback) {
        LogUtils.e("start function:querySingleHistory");
        DbManager.queryHistory(context, title, url).subscribe(new Action1<History>() {
            @Override
            public void call(History history) {
                LogUtils.e("querySingleHistory:call");
                if (history != null) {
                    if (callback != null) {
                        callback.onSuccess(history);
                    }
                } else {
                    if (callback != null) {
                        callback.onFailure("null");
                    }
                }
            }
        });
    }

    public static long addHistory(Context context, History history) {
        return DbManager.addHistory(context, history);
    }

    public static long deleteHistory(Context context, History history) {
        return DbManager.deleteHistory(context, history.getId());
    }

    public static void clearHistory(Context context) {
        DbManager.clearHistory(context);
    }

}
